package com.rrd.itext.xmlBean.layoutBean;

import java.util.List;

import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Columns")
public class XmlColumns  implements PdfElementRender{
    @XStreamAsAttribute
    @XStreamAlias("column")
    private String column;
    @XStreamAsAttribute
    @XStreamAlias("align")
    private String align;
    @XStreamAsAttribute
    @XStreamAlias("extraParagraphSpace")
    private Integer extraParagraphSpace;
    @XStreamAsAttribute
    @XStreamAlias("fixedLeading")
    private Float fixedLeading;
    @XStreamAsAttribute
    @XStreamAlias("multipliedLeading")
    private Float multipliedLeading;
    @XStreamAsAttribute
    @XStreamAlias("repeat")
    private Boolean repeat;
    
    private List<XmlHtmlBlock> htmlBlocks;
    
    @Override
    public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
        List<XmlHtmlBlock> htmlBlocks = this.getHtmlBlocks();
        if(htmlBlocks == null){
            return;
        }
        ColumnText ct = new ColumnText(cb);
        ct.setAlignment(this.getAlign());
        ct.setExtraParagraphSpace(this.getExtraParagraphSpace());
        ct.setLeading(this.getFixedLeading(), this.getMultipliedLeading());
        for(XmlHtmlBlock htmlBlock : htmlBlocks){
            List<Element> htmlElements = (List<Element>)htmlBlock.parse(builder);
            for (Element e : htmlElements) {
                ct.addElement(e);
            }
        }
        float[][] COLUMNS = getColumns(this.getColumn());
        int columnIndex = 0;
        int status = ColumnText.START_COLUMN;
        while (true) {
            ct.setSimpleColumn(COLUMNS[columnIndex][0], COLUMNS[columnIndex][1], COLUMNS[columnIndex][2], COLUMNS[columnIndex][3]);
            ct.setYLine(COLUMNS[columnIndex][3]);
            status = ct.go();
            columnIndex++;
            if(!ColumnText.hasMoreText(status)){
                break;
            }
            if (columnIndex % COLUMNS.length == 0){
                columnIndex=0;
                builder.newPage();
            }
        }
    }
    
    private float[][] getColumns(String columnDefinition){
        if(columnDefinition == null){
            return null;
        }
        String[] columns = columnDefinition.split(",");
        float[][] columnsArray = new float[columns.length][4];
        for(int i = 0; i < columns.length; i++){
            String perColumn = columns[i];
            String[] xys = perColumn.split(" ");
            for(int j = 0; j < xys.length; j++){
                columnsArray[i][j]=Integer.valueOf(xys[j]);
            }
        }
        return columnsArray;
    }
    
    public String getColumn() {
        return column;
    }
    public void setColumn(String column) {
        this.column = column;
    }
    public int getAlign() {
        if(align == null)
            return Element.ALIGN_LEFT;
        if(align.equalsIgnoreCase("center"))
            return Element.ALIGN_CENTER;
        if(align.equalsIgnoreCase("justified"))
            return Element.ALIGN_JUSTIFIED;
        if(align.equalsIgnoreCase("left"))
            return Element.ALIGN_LEFT;
        if(align.equalsIgnoreCase("right"))
            return Element.ALIGN_RIGHT;
        return Element.ALIGN_LEFT;
    }
    public void setAlign(String align) {
        this.align = align;
    }
    public Integer getExtraParagraphSpace() {
        return extraParagraphSpace==null?0:extraParagraphSpace;
    }
    public void setExtraParagraphSpace(Integer extraParagraphSpace) {
        this.extraParagraphSpace = extraParagraphSpace;
    }
    public Float getFixedLeading() {
        return fixedLeading==null?0f:fixedLeading;
    }
    public void setLeading(Float leading) {
        this.fixedLeading = leading;
    }
    public Boolean getRepeat() {
        return repeat;
    }
    public void setRepeat(Boolean repeat) {
        this.repeat = repeat;
    }
    public List<XmlHtmlBlock> getHtmlBlocks() {
        return htmlBlocks;
    }
    public void setHtmlBlocks(List<XmlHtmlBlock> htmlBlocks) {
        this.htmlBlocks = htmlBlocks;
    }
    public Float getMultipliedLeading() {
        return multipliedLeading == null?0f:multipliedLeading;
    }
    public void setMultipliedLeading(Float multipliedLeading) {
        this.multipliedLeading = multipliedLeading;
    }
}
